package com.zoe.onelink.core.web.jackson.introspect;

import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.zoe.onelink.core.property.OnelinkProperties;
import com.zoe.onelink.annotation.web.jackson.encryptor.DecryptParam;
import com.zoe.onelink.annotation.web.jackson.encryptor.EncryptParam;
import com.zoe.onelink.core.web.jackson.serializer.JacksonDecryptSerializer;
import com.zoe.onelink.core.web.jackson.serializer.JacksonEncryptSerializer;
import com.zoe.onelink.annotation.web.jackson.sentitive.SensitiveParam;
import com.zoe.onelink.core.web.jackson.sensitive.serializer.SensitiveSerializer;
import lombok.RequiredArgsConstructor;

/**
 * <p>标题: 自定义注解发现器</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-11-15
 */
@RequiredArgsConstructor
public class OnelinkJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {

    private static final long serialVersionUID = 4213983308221904103L;
    private final OnelinkProperties onelinkProperties;

    @Override
    public Object findSerializer(Annotated annotated) {
        if (annotated.hasAnnotation(EncryptParam.class)) {
            return new JacksonEncryptSerializer(onelinkProperties.getWeb().getEncrypt());
        } else if (annotated.hasAnnotation(SensitiveParam.class)) {
            return new SensitiveSerializer();
        }
        return super.findSerializer(annotated);
    }

    @Override
    public Object findDeserializer(Annotated annotated) {
        if (annotated.hasAnnotation(DecryptParam.class)) {
            return new JacksonDecryptSerializer(onelinkProperties.getWeb().getEncrypt());
        }
        return super.findDeserializer(annotated);
    }
}
